package com.librarySystem.dao;

import java.sql.PreparedStatement;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.librarySystem.dto.PageDTO;
import com.librarySystem.dto.QueryRestrictionsDTO;
import com.librarySystem.entity.Book;

@Repository
public class BookDao implements BookDaoInterface {

	@Resource
	private SessionFactory sessionFactory;
	@Resource
	private HibernateTemplate hibernateTemplate;

	@Override
	public List<Book> find(QueryRestrictionsDTO queryRestrictionsDTO,PageDTO pageDTO) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Book.class);

		if (queryRestrictionsDTO.getBookName() != null
				&& !queryRestrictionsDTO.getBookName().equals("")) {
			criteria.add(Restrictions.like("bookName", "%" + queryRestrictionsDTO
					.getBookName() + "%"));
		}


		if (queryRestrictionsDTO.getBookNumber() > 0) {
			criteria.add(Restrictions.eq("bookNo", queryRestrictionsDTO
					.getBookNumber()));
		}

		if (queryRestrictionsDTO.getBookType() > 0) {
			criteria.add(Restrictions.eq("bookType", queryRestrictionsDTO
					.getBookType()));
		}

		if (queryRestrictionsDTO.getAuthor() != null
				&& !queryRestrictionsDTO.getAuthor().equals("")) {
			criteria.add(Restrictions.like("author", "%" + queryRestrictionsDTO
					.getAuthor()+"%"));
		}
		
		criteria.setFirstResult(pageDTO.getCurrentPage()*pageDTO.getPageSize());
		criteria.setMaxResults(pageDTO.getPageSize());

		List<?> result = criteria.list();
		if (result != null && result.size() > 0) {
			return (List<Book>) result;
		}
		return null;
		
	}

	@Override
	public Book update(Book book) {
		List<Book> findByBookId = hibernateTemplate.find("from Book where bookId = ?", book.getBookId());
		if(findByBookId != null && findByBookId.size() > 0){
			return findByBookId.get(0);
		}
		return null;
	}

	@Override
	public void saveBook(Book book) {
		Session session = sessionFactory.getCurrentSession();
		session.update(book);	
	}

	@Override
	public int findCount(QueryRestrictionsDTO queryRestrictionsDTO) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Book.class);

		if (queryRestrictionsDTO.getBookName() != null
				&& !queryRestrictionsDTO.getBookName().equals("")) {
			criteria.add(Restrictions.like("bookName", "%" + queryRestrictionsDTO
					.getBookName() + "%"));
		}

		if (queryRestrictionsDTO.getBookNumber() > 0) {
			criteria.add(Restrictions.eq("bookNo", queryRestrictionsDTO
					.getBookNumber()));
		}

		if (queryRestrictionsDTO.getBookType() > 0) {
			criteria.add(Restrictions.eq("bookType", queryRestrictionsDTO
					.getBookType()));
		}

		if (queryRestrictionsDTO.getAuthor() != null
				&& !queryRestrictionsDTO.getAuthor().equals("")) {
			criteria.add(Restrictions.eq("author", "%" + queryRestrictionsDTO
					.getAuthor() + "%"));
		}

		List<?> result = criteria.list();
		
		return result.size();
	}

	@Override
	public Book findByBookNumber(Book book) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Book.class);
		if (book.getBookNo() > 0) {
			criteria.add(Restrictions.eq("bookNo",book.getBookNo()));
		}
		List<?> result = criteria.list();
		return (Book)result.get(0);
	}

	@Override
	public boolean updateBook(Book book) {
		Session session = sessionFactory.getCurrentSession();
		try {
			session.update(book);
		} catch (HibernateException e) {
			return false;
		}
		return true;
	}
	

	

}
